package cn.controller;

import cn.common.ConfigOption;
import cn.common.DocumentHandler;
import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.usualassessment.UserUATMzpdEntity;
import cn.kpi.entity.usualassseason.MzpdPassNoEntity;
import cn.kpi.entity.usualassseason.MzpdPassNoList;
import cn.kpi.entity.usualassseason.MzpdSeasonEntity;
import cn.kpi.entity.usualassseason.MzpdSeasonList;
import cn.kpi.entity.vtiondpline.VtDpUserListEntity;
import cn.kpi.service.pingyou.IPyResultService;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.usualassessment.IUserUATMzpdService;
import cn.kpi.service.vtiondpline.IVtDpUserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("useruatmzpdSeason")
public class UserUATMzpdSeasonController extends BaseController {

    @Autowired
    private IUserUATMzpdService iUserUATMzpdService;

    @Autowired
    private IPyResultService iPyResultService;

    @Autowired
    private IOrgService iOrgService;

    @Autowired
    private IVtDpUserService iVtDpUserService;

    //region 民主评定党员情况
    @RequestMapping(value = "/usualass_mzpd_season_list")
    public String UserUsassTempMzpdList(Model model) {
        try {
            List<OrgEntity> orglst = getOrgEntitiesAll();//getOrgEntities();//按权限暂时取掉
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int quarter = 1;
            switch (month) {
                case 1:
                case 2:
                case 3:
                    quarter = 4;
                    break;
                case 4:
                case 5:
                case 6:
                    quarter = 1;
                    break;
                case 7:
                case 8:
                case 9:
                    quarter = 2;
                    break;
                case 10:
                case 11:
                case 12:
                    quarter = 3;
                    break;
            }
            if (quarter == 4) year--;//默认显示上个季度
            String title = "(" + orglst.get(0).getOrgname().replace("&#12288;", "") + ")民主评议党员情况季表";
            String title1 = "(" + year + "年第" + quarter + "季度)";
            model.addAttribute("year", year);
            model.addAttribute("quarter", quarter);
            model.addAttribute("orglst", orglst);
            model.addAttribute("title", title);
            model.addAttribute("title1", title1);
        } catch (Exception ex) {

        }
        return "usualassseason/usualass_mzpd_season_List";
    }

    @RequestMapping(value = "/usualass_mzpd_season_list_admin")
    public String UserUsassTempMzpdList1(Model model, int year, int quarter) {
        try {

            String title = "民主评议党员情况季表";
            String title1 = "(" + year + "年第" + quarter + "季度)";
            model.addAttribute("year", year);
            model.addAttribute("quarter", quarter);
            model.addAttribute("orglst", null);
            model.addAttribute("title", title);
            model.addAttribute("title1", title1);
        } catch (Exception ex) {

        }
        return "usualassseason/usualass_mzpd_season_List_admin";
    }

    @LoginCheck(description = true)
    @ResponseBody
    @RequestMapping(value = "/get_season_list")
    public LayTable<MzpdSeasonEntity> GetMzpdSeasonList(HttpServletRequest request) {

        List<UserUATMzpdEntity> lst = null;
        LayTable<MzpdSeasonEntity> layTable = new LayTable<MzpdSeasonEntity>();
        try {
            int year = Integer.parseInt(request.getParameter("year"));
            int quarter = Integer.parseInt(request.getParameter("quarter"));
            String strorgid = request.getParameter("orgid");
            String where = "";
            String where2 = "";
            String where3 = "";
            String orgs = "";
            List<OrgEntity> orgEntities = GetOrgListByOrgid(Long.valueOf(strorgid));

            for (OrgEntity orgEntity : orgEntities) {
                if (orgs.equals(""))
                    orgs = String.valueOf(orgEntity.getOrgid());
                else
                    orgs += "," + orgEntity.getOrgid();
            }

            if (orgs.length() > 0) {
                where += " AND szdw in (" + orgs + ")";
                where2 += "AND orgid in(" + orgs + ")";
            }

            switch (quarter) {
                case 0:
                    break;
                case 1:
                    where3 += " AND endtime>='" + year + "-01-01 00:00:00' AND begintime<='" + year + "-03-31 23:59:59'";
//                    where3 += "AND (( begintime<='" + year + "-01-01 00:00:00' AND endtime>='" + year + "-01-01 00:00:00') OR ( begintime<='" + year + "-03-31 23:59:59' AND endtime>='" + year + "-03-31 23:59:59')) ";
                    break;
                case 2:
                    where3 += " AND endtime>='" + year + "-04-01 00:00:00' AND begintime<='" + year + "-06-30 23:59:59'";
//                    where3 += " AND (( begintime<='" + year + "-04-01 00:00:00' AND endtime>='" + year + "-04-01 00:00:00') OR ( begintime<='" + year + "-06-30 23:59:59' AND endtime>='" + year + "-06-30 23:59:59')) ";
                    break;
                case 3:
                    where3 += " AND endtime>='" + year + "-07-01 00:00:00' AND begintime<='" + year + "-09-30 23:59:59'";
//                    where3 += "AND (( begintime<='" + year + "-07-01 00:00:00' AND endtime>='" + year + "-07-01 00:00:00') OR ( begintime<='" + year + "-09-30 23:59:59' AND endtime>='" + year + "-09-30 23:59:59')) ";
                    break;
                case 4:
                    where3 += " AND endtime>='" + year + "-10-01 00:00:00' AND begintime<='" + year + "-12-31 23:59:59'";
//                    where3 += "AND (( begintime<='" + year + "-10-01 00:00:00' AND endtime>='" + year + "-10-01 00:00:00') OR ( begintime<='" + year + "-12-31 23:59:59' AND endtime>='" + year + "-12-31 23:59:59')) ";
                    break;
            }

            //todo:换季修改，临时
            if (quarter == 3 || quarter == 4)
                lst = iUserUATMzpdService.GetAllUserMzpdListProcess(where, where3, year, quarter);

            if (quarter == 2 || quarter == 1) {
                lst = iUserUATMzpdService.GetAllUserMzpdListProcess(where, where3, year, quarter);
                where3 += " AND (vi.zzpdcllx='不参加考核' OR vi.mzpdcllx='不参加考核') AND status = 1 ";
                List<VtDpUserListEntity> nocheckelst = iVtDpUserService.GetUserVtDpBcjkhList(where3);
                Map<Long, Integer> nojp = new HashMap<>();
                for (VtDpUserListEntity vtDpUserListEntity : nocheckelst) {
                    UserUATMzpdEntity userUATMzpdEntity = new UserUATMzpdEntity();
                    userUATMzpdEntity.setWjcount(1L);
                    userUATMzpdEntity.setGzdw(vtDpUserListEntity.getOrgname());
                    userUATMzpdEntity.setOrgname(vtDpUserListEntity.getOrgname());
                    userUATMzpdEntity.setCardno(vtDpUserListEntity.getCardno());
                    userUATMzpdEntity.setUserid(vtDpUserListEntity.getUserid());
                    userUATMzpdEntity.setYear(year);
                    userUATMzpdEntity.setQuarter(quarter);
                    userUATMzpdEntity.setStatus(-1);
                    userUATMzpdEntity.setZzmm(vtDpUserListEntity.getZzmm());// todo:临时处理
                    userUATMzpdEntity.setRealname(vtDpUserListEntity.getRealname());
                }
            }

            /*按照组织机构分组**/
            LinkedHashMap<String, List<UserUATMzpdEntity>> mzpdMap = new LinkedHashMap<>();
            for (UserUATMzpdEntity mzpdVo : lst) {
                if (mzpdVo.getZzmm() != null && (mzpdVo.getZzmm().equals("中共党员") || mzpdVo.getZzmm().equals("中共预备党员"))) {
                    String gzdw = mzpdVo.getGzdw();
                    if(gzdw==null||gzdw.equals("")) gzdw = mzpdVo.getOrgname();//给实际工作单位赋值

                    List<UserUATMzpdEntity> tempList = mzpdMap.get(gzdw);//按照实际工作单位来分
                    /*如果取不到数据,那么直接new一个空的ArrayList**/
                    if (tempList == null) {
                        tempList = new ArrayList<>();
                        tempList.add(mzpdVo);
                        mzpdMap.put(gzdw, tempList);
                    } else {
                        /*某个sku之前已经存放过了,则直接追加数据到原来的List里**/
                        tempList.add(mzpdVo);
                    }
                }
            }
            /*统计机构数据，形成新的实体类*/
            List<MzpdSeasonEntity> mzpdSeasonEntities = new ArrayList<>();
            MzpdSeasonEntity sumitem = new MzpdSeasonEntity();
            sumitem.setOrgname("合计");
            for (String s : mzpdMap.keySet()) {
                MzpdSeasonEntity mzs = new MzpdSeasonEntity();
                mzs.setOrgname(s);
                for (UserUATMzpdEntity userUATMzpdEntity : mzpdMap.get(s)) {
                    mzs.setWorkCParty(mzs.getWorkCParty() + 1);//全部统计
//					if(userUATMzpdEntity.getZzmm().equals("中共预备党员")){
//						mzs.setJoinUnLevelCParty(mzs.getJoinUnLevelCParty() + 1);
//					}
//					else {//正式党员参与统计
//						//不参与人数修改
                    if (userUATMzpdEntity.getWjcount() != null && userUATMzpdEntity.getWjcount() > 0) {
                        mzs.setNotJoinCCParty(mzs.getNotJoinCCParty() + 1);
                    }
                    String mzpdRe = userUATMzpdEntity.getDymzpyjg();
                    if (mzpdRe != null) {
                        switch (mzpdRe) {
                            case "优秀":
                                mzs.setGood(mzs.getGood() + 1);
                                break;
                            case "合格":
                                mzs.setPass(mzs.getPass() + 1);
                                break;
                            case "基本合格":
                                mzs.setJustPass(mzs.getJustPass() + 1);
                                break;
                            case "不合格":
                                mzs.setNotPass(mzs.getNotPass() + 1);
                                break;
                            case "不定等次":
                                mzs.setJoinUnLevelCParty(mzs.getJoinUnLevelCParty() + 1);
                                break;
                        }
                    }
//					}
                }
                mzpdSeasonEntities.add(mzs);
//				sumitem.setWorkCParty(sumitem.getWorkCParty()+mzs.getWorkCParty());
                sumitem.setGood(sumitem.getGood() + mzs.getGood());
                sumitem.setPass(sumitem.getPass() + mzs.getPass());
                sumitem.setJustPass(sumitem.getJustPass() + mzs.getJustPass());
                sumitem.setNotPass(sumitem.getNotPass() + mzs.getNotPass());
                sumitem.setNotJoinCCParty(sumitem.getNotJoinCCParty() + mzs.getNotJoinCCParty());
                sumitem.setJoinUnLevelCParty(sumitem.getJoinUnLevelCParty() + mzs.getJoinUnLevelCParty());
            }

            //取出记录表，如果有党员人数，就替换党员人数
            List<Map> old = new ArrayList<>();
            HashMap<String, Integer> orgParty = new HashMap<>();
            old = iPyResultService.GetOldOrgPyResultTjList(where2, year, quarter);
            Integer sum = 0;
            for (int i = 0; i < old.size(); i++) {
                Map<String, Object> map = old.get(i);
                if (map.get("orgname") != null) {
                    Integer dyrsint = 0;
                    if (map.get("dyrs") != null) dyrsint = Integer.valueOf(map.get("dyrs").toString());
                    orgParty.put(map.get("orgname").toString(), dyrsint);
                    sum += dyrsint;
                }
            }


            sumitem.setWorkCParty(sum);
            mzpdSeasonEntities.add(0, sumitem);//放到最上面
            for (int i = 0; i < mzpdSeasonEntities.size(); i++) {
                String orgname = mzpdSeasonEntities.get(i).getOrgname();
                if (orgParty.containsKey(orgname)) {
                    Integer realdy = orgParty.get(orgname);
                    mzpdSeasonEntities.get(i).setWorkCParty(realdy);
                }
                mzpdSeasonEntities.get(i).setRowNum(i + 1);
            }


            int iCount = mzpdSeasonEntities.size();
            layTable.setCode(0);
            layTable.setCount(iCount);
            layTable.setData(mzpdSeasonEntities);

        } catch (Exception e) {
            layTable.setCode(500);
        }
        return layTable;
    }

    //导出

    @RequestMapping(value = "/export_season_mzpd", method = RequestMethod.POST)
    @ResponseBody
    public ExceResult ExportSeasonMzpd(@RequestBody MzpdSeasonList uatmzpdsummary) {
        ExceResult result = new ExceResult();
        result.setCode(400);
        result.setResult(0);
        try {
            List<MzpdSeasonEntity> mzpdSeasonEntities = uatmzpdsummary.getUatmzpdsummary();
            String orgName = uatmzpdsummary.getOrgname();
            String year = uatmzpdsummary.getYearid();
//			String orgid = uatmzpdsummary.getOrgid();
            String quarter = uatmzpdsummary.getQuarter();
            String Name = orgName + "民主评议党员情况季表(" + year + "年" + quarter + "季)";

            MzpdSeasonEntity sumitem = mzpdSeasonEntities.get(0);//合计项取出
            mzpdSeasonEntities.remove(0);

            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("orgname", orgName);
            dataMap.put("yearid", year);
            dataMap.put("quarter", quarter);
            dataMap.put("workCParty", sumitem.getWorkCParty().toString());
            dataMap.put("joinCCParty", sumitem.getJoinCCParty().toString());
            dataMap.put("notJoinCCParty", sumitem.getNotJoinCCParty().toString());
            dataMap.put("joinUnLevelCParty", sumitem.getJoinUnLevelCParty().toString());
            dataMap.put("good", sumitem.getGood().toString());
            dataMap.put("pass", sumitem.getPass().toString());
            dataMap.put("justPass", sumitem.getJustPass().toString());
            dataMap.put("notPass", sumitem.getNotPass().toString());

            //循环赋予值（表数据）
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            for (MzpdSeasonEntity mzpdSeasonEntity : mzpdSeasonEntities) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("rowNum", String.valueOf(mzpdSeasonEntity.getRowNum() - 1));
                map.put("orgname", mzpdSeasonEntity.getOrgname());
                map.put("workCParty", mzpdSeasonEntity.getWorkCParty().toString());
                map.put("joinCCParty", mzpdSeasonEntity.getJoinCCParty().toString());
                map.put("notJoinCCParty", mzpdSeasonEntity.getNotJoinCCParty().toString());
                map.put("joinUnLevelCParty", mzpdSeasonEntity.getJoinUnLevelCParty().toString());
                map.put("good", mzpdSeasonEntity.getGood().toString());
                map.put("pass", mzpdSeasonEntity.getPass().toString());
                map.put("justPass", mzpdSeasonEntity.getJustPass().toString());
                map.put("notPass", mzpdSeasonEntity.getNotPass().toString());
                list.add(map);
            }
            dataMap.put("table", list);


            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

            DocumentHandler doc = new DocumentHandler();
            String fileName = Name + "_" + sdf.format(new Date()) + ".doc";
            String filePath = ConfigOption.getInstance().getFileurl() + "/" + fileName;
            String tempfilename = "dymzpd.xml";//"mzpdtemp.xml";//换成导出的模板
            Boolean bolResult = doc.createObjDoc(dataMap, filePath, tempfilename);

            if (bolResult) {
                String url = ConfigOption.getInstance().getWeburl() + fileName;
                result.setCode(200);
                result.setResult(1);
                result.setMsg(url);
            } else {
                result.setCode(300);
                result.setResult(0);
                result.setMsg("操作失败");
            }
        } catch (Exception ex) {
            result.setCode(500);
            result.setResult(0);
            result.setMsg("操作异常");
            ex.printStackTrace();
        }
        return result;
    }
    //endregion

    //region 合格不合格
    @RequestMapping(value = "/editMzpd")
    public String editMzpd(Model model, String mzpdId, int year, int quarter, int orgid, String cardno) {

        UserUATMzpdEntity entity = new UserUATMzpdEntity();
        if (!mzpdId.equals("null")) {
            List<UserUATMzpdEntity> list = iUserUATMzpdService.getMzpdById(Integer.parseInt(mzpdId));
            if (list.size() > 0) {
                entity = list.get(0);

            }
        }
        model.addAttribute("entity", entity);
        model.addAttribute("orgid", orgid);
        model.addAttribute("cardno", cardno);
        model.addAttribute("year", year);
        model.addAttribute("quarter", quarter);

        return "usualassseason/usualass_mzpd_edit";


    }

    @LoginCheck(description = true)
    @ResponseBody
    @RequestMapping(value = "/updateMzpd")
    public String updateMzpd(HttpServletRequest request, HttpServletResponse response) {


        try {

            String reason = request.getParameter("reason");
            String howtodo = request.getParameter("howtodo");
            String remark = request.getParameter("remark");
            String cardno = request.getParameter("cardno");
            String quarter = request.getParameter("quarter").toString();
            Long orgid = Long.parseLong(request.getParameter("orgid").toString());
            String year = request.getParameter("yearid").toString();
            String mzpdpassnoid = request.getParameter("mzpdpassnoid");
            int result = 0;
            if (mzpdpassnoid.trim() != "") {

                result = iUserUATMzpdService.updateMzpd(reason, howtodo, remark, Integer.parseInt(mzpdpassnoid));
            } else {
                List<MzpdPassNoEntity> list = new ArrayList<>();
                MzpdPassNoEntity mzpdPassNoEntity = new MzpdPassNoEntity();
                mzpdPassNoEntity.setStatus(0);
                mzpdPassNoEntity.setHowtodo(howtodo);
                mzpdPassNoEntity.setReason(reason);
                mzpdPassNoEntity.setRemark(remark);
                mzpdPassNoEntity.setCardno(cardno);
                mzpdPassNoEntity.setOrgid(orgid);
                mzpdPassNoEntity.setYear(year);
                mzpdPassNoEntity.setQuarter(quarter);

                list.add(mzpdPassNoEntity);
                result = iUserUATMzpdService.UpdateMzpdpassEntity(list, "");
            }


            return renderStringScript(response, "<script>window.parent.closeD(" + result + ")</script>");
        } catch (Exception ex) {

            return renderStringScript(response, "<script>window.parent.closeD(0)</script>");
        }

    }


    @RequestMapping(value = "/updateStatus")
    public void updateStatus(HttpServletRequest request, HttpServletResponse response) {

        boolean rs = false;
        try {
            int year = Integer.parseInt(request.getParameter("year"));
            int quarter = Integer.parseInt(request.getParameter("quarter"));
            Long orgid = Long.parseLong(request.getParameter("orgid"));
            int status = Integer.parseInt(request.getParameter("status"));

            iUserUATMzpdService.updateMzpdStatus(status, orgid, year, quarter);
            rs = true;
        } catch (Exception ex) {


            System.out.print(ex.getMessage());

        }

        // return  rs;
    }


    //region 合格不合格
    @RequestMapping(value = "/usualass_mzpd_passno_list")
    public String UserMzpdpassnoList(Model model) {
        try {
            List<OrgEntity> orglst = getOrgEntities();//按权限暂时取掉
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int quarter = 1;
            UserEntity userEntity = this.GetLoginUser();
            switch (month) {
                case 1:
                case 2:
                case 3:
                    quarter = 4;
                    break;
                case 4:
                case 5:
                case 6:
                    quarter = 1;
                    break;
                case 7:
                case 8:
                case 9:
                    quarter = 2;
                    break;
                case 10:
                case 11:
                case 12:
                    quarter = 3;
                    break;
            }
            if (quarter == 4) year--;//默认上个季度
            String title = "(" + orglst.get(0).getOrgname().replace("&#12288;", "") + ")单位" +
                    year + "年" + quarter + "季度“基本合格”、“不合格”党员基本情况统计分析表";
            model.addAttribute("year", year);
            model.addAttribute("quarter", quarter);
            model.addAttribute("orglst", orglst);
            model.addAttribute("title", title);
            model.addAttribute("uType", userEntity.getUsertype());
        } catch (Exception ex) {

        }
        return "usualassseason/usualass_mzpd_passno_List";
    }


    //region 合格不合格
    @RequestMapping(value = "/newusualass_mzpd_passno_list")
    public String UserMzpdpassnoList1(Model model) {
        try {
            List<OrgEntity> orglst = getOrgEntities();//按权限暂时取掉
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int quarter = 1;
            UserEntity userEntity = this.GetLoginUser();
            switch (month) {
                case 1:
                case 2:
                case 3:
                    quarter = 4;
                    break;
                case 4:
                case 5:
                case 6:
                    quarter = 1;
                    break;
                case 7:
                case 8:
                case 9:
                    quarter = 2;
                    break;
                case 10:
                case 11:
                case 12:
                    quarter = 3;
                    break;
            }
            if (quarter == 4) year--;//默认上个季度
            String title = "(" + orglst.get(0).getOrgname().replace("&#12288;", "") + ")单位" +
                    year + "年" + quarter + "季度“基本合格”、“不合格”党员基本情况统计分析表";
            model.addAttribute("year", year);
            model.addAttribute("quarter", quarter);
            model.addAttribute("orglst", orglst);
            model.addAttribute("title", title);
            model.addAttribute("uType", userEntity.getUsertype());
        } catch (Exception ex) {

        }
        return "usualassseason/usualass_mzpd_passno_List1";
    }


    @LoginCheck(description = true)
    @ResponseBody
    @RequestMapping(value = "/get_passno_list")
    public LayTable<MzpdPassNoEntity> GetMzpdPassNoList(HttpServletRequest request) {
        List<UserUATMzpdEntity> lst = null;
        LayTable<MzpdPassNoEntity> layTable = new LayTable<MzpdPassNoEntity>();
        try {
            int year = Integer.parseInt(request.getParameter("year"));
            int quarter = Integer.parseInt(request.getParameter("quarter"));
            String strorgid = request.getParameter("orgid");
            String where = "";
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));

            String orgs = "";
            List<OrgEntity> orgEntities = GetOrgListByOrgid(Long.valueOf(strorgid));

            for (OrgEntity orgEntity : orgEntities) {
                if (orgs.equals(""))
                    orgs = String.valueOf(orgEntity.getOrgid());
                else
                    orgs += "," + orgEntity.getOrgid();
            }

            if (orgs.length() > 0) {
                where += " AND szdw in (" + orgs + ")";
            }
            int userType = this.GetLoginUser().getUsertype();

            lst = iUserUATMzpdService.GetMzpdPassNoEntity(where, year, quarter);

            List<MzpdPassNoEntity> mzpdSeasonEntities = new ArrayList<>();
            for (UserUATMzpdEntity userUATMzpdEntity : lst) {
                if (userUATMzpdEntity.getZzmm() != null && (userUATMzpdEntity.getZzmm().equals("中共党员") || userUATMzpdEntity.getZzmm().equals("中共预备党员"))) {
                    MzpdPassNoEntity mzs = new MzpdPassNoEntity();
                    String mzpdRe = userUATMzpdEntity.getDymzpyjg();
                    if (mzpdRe != null && (mzpdRe.equals("基本合格") || mzpdRe.equals("不合格"))) {
                        mzs.setMzpdpassnoid(userUATMzpdEntity.getMzpdpassnoid());
                        mzs.setOrgid(userUATMzpdEntity.getOrgid());
                        mzs.setOrgname(userUATMzpdEntity.getOrgname());
                        mzs.setRealname(userUATMzpdEntity.getRealname());
                        mzs.setCardno(userUATMzpdEntity.getCardno());
                        mzs.setSex(userUATMzpdEntity.getSex());
                        mzs.setWorklevel(userUATMzpdEntity.getRank());
                        mzs.setPdlevel(mzpdRe);
                        mzs.setStatus(userUATMzpdEntity.getMzpdStatus());
                        mzs.setBirthday(userUATMzpdEntity.getBirthday());
                        mzs.setJoinpartytime(userUATMzpdEntity.getJoinpartytime());
                        mzs.setWorkTime(userUATMzpdEntity.getCjgagzsj());

                        switch (userType) {

                            case 3:
                                if (userUATMzpdEntity.getMzpdStatus() != 0) {

                                    mzs.setReason(userUATMzpdEntity.getReason());
                                    mzs.setHowtodo(userUATMzpdEntity.getHowtodo());

                                }
                                break;
                            default:

                                mzs.setReason(userUATMzpdEntity.getReason());
                                mzs.setHowtodo(userUATMzpdEntity.getHowtodo());
                                break;

                        }
                        mzs.setRemark(userUATMzpdEntity.getRemark());
                        mzs.setMzpdpassnoid(userUATMzpdEntity.getMzpdpassnoid());
                        mzpdSeasonEntities.add(mzs);
                    }

                }
            }

            for (int i = 0; i < mzpdSeasonEntities.size(); i++) {
                mzpdSeasonEntities.get(i).setRowNum(i + 1);
            }

            int iCount = orgEntities.size();
            layTable.setCode(0);
            layTable.setCount(iCount);
            layTable.setData(mzpdSeasonEntities);

        } catch (
                Exception e)

        {
            layTable.setCode(500);
        }
        return layTable;
    }


    @ResponseBody
    @RequestMapping(value = "/newget_passno_list")
    public LayTable<MzpdPassNoEntity> GetMzpdPassNoList1(HttpServletRequest request) {
        List<UserUATMzpdEntity> lst = null;
        LayTable<MzpdPassNoEntity> layTable = new LayTable<MzpdPassNoEntity>();
        try {
            int year = Integer.parseInt(request.getParameter("year"));


            String where = "";


            String orgs = "";
            List<OrgEntity> orgEntities = GetOrgListByOrgid(this.GetLoginUser().getSzdw());

            for (OrgEntity orgEntity : orgEntities) {
                if (orgs.equals(""))
                    orgs = String.valueOf(orgEntity.getOrgid());
                else
                    orgs += "," + orgEntity.getOrgid();
            }

            if (orgs.length() > 0) {
                where += " AND szdw in (" + orgs + ")";
            }
            int userType = this.GetLoginUser().getUsertype();

            lst = iUserUATMzpdService.GetMzpdPassNoEntity1("", year);

            List<MzpdPassNoEntity> mzpdSeasonEntities = new ArrayList<>();
            for (UserUATMzpdEntity userUATMzpdEntity : lst) {
                if (userUATMzpdEntity.getZzmm() != null && (userUATMzpdEntity.getZzmm().equals("中共党员") || userUATMzpdEntity.getZzmm().equals("中共预备党员"))) {
                    MzpdPassNoEntity mzs = new MzpdPassNoEntity();
                    String mzpdRe = userUATMzpdEntity.getDymzpyjg();
                    if (mzpdRe != null && (mzpdRe.equals("基本合格") || mzpdRe.equals("不合格"))) {
                        mzs.setMzpdpassnoid(userUATMzpdEntity.getMzpdpassnoid());
                        mzs.setOrgid(userUATMzpdEntity.getOrgid());
                        mzs.setOrgname(userUATMzpdEntity.getOrgname());
                        mzs.setRealname(userUATMzpdEntity.getRealname());
                        mzs.setCardno(userUATMzpdEntity.getCardno());
                        mzs.setSex(userUATMzpdEntity.getSex());
                        mzs.setWorklevel(userUATMzpdEntity.getRank());
                        mzs.setPdlevel(mzpdRe);
                        mzs.setStatus(userUATMzpdEntity.getMzpdStatus());
                        mzs.setBirthday(userUATMzpdEntity.getBirthday());
                        mzs.setJoinpartytime(userUATMzpdEntity.getJoinpartytime());
                        mzs.setWorkTime(userUATMzpdEntity.getCjgagzsj());

                        switch (userType) {

                            case 3:
                                if (userUATMzpdEntity.getMzpdStatus() != 0) {

                                    mzs.setReason(userUATMzpdEntity.getReason());
                                    mzs.setHowtodo(userUATMzpdEntity.getHowtodo());

                                }
                                break;
                            default:

                                mzs.setReason(userUATMzpdEntity.getReason());
                                mzs.setHowtodo(userUATMzpdEntity.getHowtodo());
                                break;

                        }
                        mzs.setRemark(userUATMzpdEntity.getRemark());
                        mzs.setMzpdpassnoid(userUATMzpdEntity.getMzpdpassnoid());
                        mzpdSeasonEntities.add(mzs);
                    }

                }
            }

            for (int i = 0; i < mzpdSeasonEntities.size(); i++) {
                mzpdSeasonEntities.get(i).setRowNum(i + 1);
            }

            int iCount = orgEntities.size();
            layTable.setCode(0);
            layTable.setCount(iCount);
            layTable.setData(mzpdSeasonEntities);

        } catch (
                Exception e)

        {
            layTable.setCode(500);
        }
        return layTable;
    }




    //导出
    @ResponseBody
    @RequestMapping(value = "/export_passno_mzpd", method = RequestMethod.POST)
    public ExceResult ExportPassMzpd(@RequestBody MzpdPassNoList uatmzpdsummary) {
        ExceResult result = new ExceResult();
        result.setCode(400);
        result.setResult(0);
        try {
            List<MzpdPassNoEntity> mzpdPassNoEntities = uatmzpdsummary.getUatmzpdsummary();
            String orgName = uatmzpdsummary.getOrgname();
            String year = uatmzpdsummary.getYearid();
            String quarter = uatmzpdsummary.getQuarter();
            String Name = "(" + orgName + ")单位" +
                    year + "年" + quarter + "季度“基本合格”、“不合格”党员基本情况统计分析表";

            DateFormat dFormat = new SimpleDateFormat("yyyy年MM月");
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("orgname", orgName);
            dataMap.put("yearid", year);
            dataMap.put("quarter", quarter);

            //循环赋予值（表数据）
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            for (MzpdPassNoEntity mzpdPassNoEntity : mzpdPassNoEntities) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("rowNum", String.valueOf(mzpdPassNoEntity.getRowNum()));
                map.put("orgname", mzpdPassNoEntity.getOrgname());
                map.put("realname", mzpdPassNoEntity.getRealname().toString());
                map.put("cardno", mzpdPassNoEntity.getCardno().toString());
                map.put("sex", getsexN(mzpdPassNoEntity.getSex()));
                String bd = dFormat.format(mzpdPassNoEntity.getBirthday());
                map.put("birthday", bd);
                String workTime = dFormat.format(mzpdPassNoEntity.getWorkTime());
                map.put("workTime", workTime);
                String jt = dFormat.format(mzpdPassNoEntity.getJoinpartytime());
                map.put("joinpartytime", jt);
                map.put("worklevel", getxrzwn(mzpdPassNoEntity.getWorklevel()));
                map.put("pdlevel", mzpdPassNoEntity.getPdlevel().toString());
                if (mzpdPassNoEntity.getReason() == null) mzpdPassNoEntity.setReason("");
                map.put("reason", mzpdPassNoEntity.getReason().toString());
                if (mzpdPassNoEntity.getHowtodo() == null) mzpdPassNoEntity.setHowtodo("");
                map.put("howtodo", mzpdPassNoEntity.getHowtodo().toString());
                if (mzpdPassNoEntity.getRemark() == null) mzpdPassNoEntity.setRemark("");
                map.put("remark", mzpdPassNoEntity.getRemark().toString());
                list.add(map);
            }
            dataMap.put("table", list);


            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

            DocumentHandler doc = new DocumentHandler();
            String fileName = Name + "_" + sdf.format(new Date()) + ".doc";
            String filePath = ConfigOption.getInstance().getFileurl() + "/" + fileName;
            String tempfilename = "jbhgbhg.xml";//"mzpdtemp.xml";//换成导出的模板
            Boolean bolResult = doc.createObjDoc(dataMap, filePath, tempfilename);

            if (bolResult) {
                String url = ConfigOption.getInstance().getWeburl() + fileName;
                result.setCode(200);
                result.setResult(1);
                result.setMsg(url);
            } else {
                result.setCode(300);
                result.setResult(0);
                result.setMsg("操作失败");
            }
        } catch (Exception ex) {
            result.setCode(500);
            result.setResult(0);
            result.setMsg("操作异常");
            ex.printStackTrace();
        }
        return result;
    }


    private String getsexN(Integer sex) {
        String ret = "";
        switch (sex) {
            case 0:
                ret = "男";
                break;
            case 1:
                ret = "女";
                break;
        }
        return ret;
    }

    private String getxrzwn(String wn) {
        if (wn == null) return "";
        String re = "";
        switch (wn) {
            case "1":
                re = "处级领导干部";
                break;
            case "2":
                re = "科级领导干部";
                break;
            case "3":
                re = "民警";
                break;
            case "4":
                re = "未定职务(新录用公务员)";
                break;
            case "5":
                re = "领导班子";
                break;

        }
        return re;
    }

    //保存
    @RequestMapping(value = "/mzpd_passno_save", method = RequestMethod.POST)
    @ResponseBody
    public String MzpdPassSave(@RequestBody MzpdPassNoList uatmzpdsummary) throws Exception {
        String year = uatmzpdsummary.getYearid();
        String quarter = uatmzpdsummary.getQuarter();
        List<MzpdPassNoEntity> assessRulePays = uatmzpdsummary.getUatmzpdsummary();
        String id = "";
        for (MzpdPassNoEntity assessRulePay : assessRulePays) {
            assessRulePay.setYear(year);
            assessRulePay.setQuarter(quarter);
            if (id.equals("") && assessRulePay.getMzpdpassnoid() != null) {
                id = String.valueOf(assessRulePay.getMzpdpassnoid());
            } else if (assessRulePay.getMzpdpassnoid() != null) {
                id += "," + String.valueOf(assessRulePay.getMzpdpassnoid());
            }
        }

        String where = "";
        if (!id.equals(""))//不带组织机构就不删除
            where = "AND mzpdpassnoid IN (" + id + ") ";// AND yearid ="+year +" AND quarter ="+ quarter;

        int iResult = iUserUATMzpdService.UpdateMzpdpassEntity(assessRulePays, where);
        return String.valueOf(iResult);
    }

    //endregion

}
